/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pehulja.mmdo.coursework.math.checkingResults;

import com.pehulja.mmdo.coursework.math.SimplexTable;
import com.pehulja.mmdo.coursework.math.Solver;
import com.pehulja.mmdo.coursework.math.matrix.Matrix;
import com.pehulja.mmdo.coursework.math.matrix.MatrixMathematics;
import com.pehulja.mmdo.coursework.math.matrix.NoSquareException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.apache.commons.math.linear.RealMatrix;

/**
 *
 * @author pehulja
 */
public class MusculoskeletalChecking{
    private boolean resultChecking = false;
    
    private Solver solver;
    private RealMatrix matrixAfs;
    private int determinant;
    private Thread calculation;
    private JLabel field;
    private static MusculoskeletalChecking checking = new MusculoskeletalChecking();
    
    private MusculoskeletalChecking() {}
    
    public static MusculoskeletalChecking getChecking(){
        return checking;       
    }
    
    public void checkingForTheFoothold() throws NoSquareException{
        if(solver!=null){
            calculation = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("In determinant checking thread");
                    matrixAfs = solver.getCanonicalLinearProgramTask().getBusinessRestrictionsMatrix(solver.getBasisVectorsIdExtended(), false);
                    try {
                        determinant = (int) MatrixMathematics.determinant(new Matrix(matrixAfs.getData()));
                        resultChecking = determinant!=0;
                        field.setText(resultChecking?"Положительный":"отрицательный");
                    } catch (NoSquareException ex) {
                        Logger.getLogger(MusculoskeletalChecking.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            calculation.start();
        }
    }

    public void setSolver(Solver solver) {
        this.solver = solver;
    }
    
    public RealMatrix getMatrixAfs() {
        return matrixAfs;
    }

    public int getDeterminant() {
        return determinant;
    }
    
    public boolean isFinished(){
        return !calculation.isAlive();
    }

    public void setField(JLabel field) {
        this.field = field;
    }

    public Boolean getResult() {
        if(!isFinished())
            return null;
        else            
            return resultChecking;
    }
    
}
